package com.skt.common_hr.overtime.utils
{
  	import flash.net.URLRequest;
  	import flash.net.URLRequestMethod;
  	import flash.net.URLVariables;
  	import flash.net.navigateToURL;
  	
  	import mx.collections.IViewCursor;
  	import mx.controls.DataGrid;
  	import mx.controls.dataGridClasses.DataGridColumn;
	    
	
	public class DataGridDataExporter
	{
		public static function exportCSV(dg:DataGrid, 
        								 	jspFile:String, 
         									columnSeparator:String="\t", 
         									rowSeparator:String="\n"):void
		{
			var data:String = "";
			var columns:Array = dg.columns;
			var columnCount:int = columns.length;
			var column:DataGridColumn;
			var header:String = "";
			var headerGenerated:Boolean = false;
			var dataProvider:Object = dg.dataProvider;
			
			var rowCount:int = dataProvider.length;
			var dp:Object = null;

  
			var cursor:IViewCursor = dataProvider.createCursor ();
			var j:int = 0;
         
			header ='<table><thead>';
			data   = '<tr >';
         
			//loop through rows
			while (!cursor.afterLast)
			{
				var obj:Object = null;
				obj = cursor.current;

				if (headerGenerated)
				data += "<tr >";
               
				//loop through all columns for the row
				for(var k:int = 0; k < columnCount; k++)
				{
					column = columns[k];

					// Don't add if data is invisible
					if(!column.visible || column.dataField==null)
					{                  
						continue;
               		}
            
               		//data += "<td >"+ column.itemToLabel(obj)+ "</td >";
               		data += "<td >"+ filteringData(column.itemToLabel(obj))+ "</td >";

               		//generate header of TSV, only if it's not genereted yet
               		if (!headerGenerated)
               		{
                  		header += "<th >" + column.headerText + "</th >";
               		}            
            	}
            
            	if (!headerGenerated){
              		header += "</tr></thead><tbody>"; 
               		headerGenerated = true;
            	}

				data += "</tr >";
 				j++;
				cursor.moveNext ();
         	}
         
	         data += "</tbody></table >"
	         //set references to null:
	         dataProvider = null;
	         columns = null;
	         column = null;         
         
         
			//////////////////////////////////////////// 
			// Start opening into a new browser
			var variables:URLVariables = new URLVariables(); 
			variables.dg   = header + "\r\n" + data;
			 
			//Setup a new request and make sure that we are 
			//sending the data through a post
			var u:URLRequest = new URLRequest(jspFile);
			u.data = variables; //Pass the variables
			u.method = URLRequestMethod.POST; //Don't forget that we need to send as POST
			 
			//We can use _self here, since the script will through a filedownload header
			//which results in offering a download to the user (and still remaining in you Flex app.)
			navigateToURL(u,"_self");
		}   
		
		public static function filteringData(src:String):String
		{
			if(isNaN(parseFloat(src))) {
				var arr:Array = src.split(":");
				if(arr.length>1) {
					if(arr[1] == "2") {
						return arr[0] + "(결재중)";
					}
					else if(arr[1] == "3") {
						return arr[0];
					}
				}
												
			}	
			return src;
		}
	}
}